package com.hai.boot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import jakarta.servlet.http.HttpSession;

@Controller
public class LoginController {
	
	@PostMapping("/login")
	public String loginUp(@RequestParam("username") String username,
						@RequestParam("password") String password,
						HttpSession session,
						Model model) {
		
		if (username.equals("admin") && password.equals("123admin")) {
				session.setAttribute("User", username);
				return "redirect:/back";	
		}
		model.addAttribute("msg", "账户或者密码错误");
		return "signup";
	}
	@GetMapping("/out/login")
	public String OutLogin(HttpSession session) {
		session.invalidate();
		return "redirect:/";
	}
	@GetMapping("/register")
	public String Register(HttpSession session) {
		session.invalidate();
		return "register";
	}
	@PostMapping("/Register")
	public String RegisterUp(@RequestParam("username") String username,
						  @RequestParam("password") String password,
						  HttpSession session,
						  Model model) {

		if (username.equals("") && password.equals("123admin")) {
			session.setAttribute("User", username);
			return "redirect:/back";
		}
		model.addAttribute("msg", "账户或者密码非空");
		return "register";
	}

}
